package com.winning.emr.base.aspect;

import com.winning.emr.base.utils.StringUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author chensj
 * @title
 * @email chensj@winning.com.cn
 * @package com.winning.emr.base.aspect
 * @date: 2018-12-17 21:24
 */
@Aspect
@Component
public class DaoErrorAspect {

    private static final Logger logger = LoggerFactory.getLogger(DaoErrorAspect.class);
    /**
     * 定义切点
     */
    @Pointcut("execution(* com.winning.emr.dao..*(..))")
    public void daoErrorPointcut(){}

    /**
     * DAO层抛出的异常在这边捕获
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value = "daoErrorPointcut()",throwing = "e")
    public void handleThrowing(JoinPoint joinPoint, Exception e) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        String allName = joinPoint.getSignature().toString();
        allName = allName.split(" ")[1];
        //调用方法类名
        String className = allName.substring(0, allName.lastIndexOf("."));
        //转换DAO名称到数据子集名称
        String modelName = className.substring(className.lastIndexOf(".")+1).replace("Dao","");
        String dataType = StringUtil.HumpToUnderline(modelName).substring(1);
//        MbkDataError error = new MbkDataError();
//        error.setClassName(className);
//        error.setMethodName(methodName);
//        error.setSourceName(dataType);
//        error.setArgumentString(JSONArray.toJSON(args).toString());
//        error.setErrorMessage(e.getMessage());
//        error.setErrorTime(new Date());
//        mbkDataErrorService.createMbkDataError(error);
        logger.error("异常所在类：[{}],异常所在方法：[{}],异常中的参数:[{}],异常:[{}]\n\r",className,methodName,args,e.getMessage());
    }

}
